const childProcess = require('child_process')
const {
  baseDir,
  getModules,
  colorLog,
} = require('./utils')
const fs = require('fs')
const path = require('path')

let modules = []

// 加载指定的几个项目名称，格式为：["qiankun-demo-app1"]
const modulesToRunConfigPath = path.join(__dirname, './modules-to-run.json')
if (fs.existsSync(modulesToRunConfigPath)) {
  modules = require('./modules-to-run.json')
} else {
  modules = getModules()
}

console.log('modules', modules)

let timeout = 0
modules.forEach(item => {
  setTimeout(() => {
    const dir = baseDir + item
    const appName = item

    const process = childProcess.spawn('yarn serve', [], {
      cwd: dir,
      shell: true
    })
    process.stdout.on('data', function (data) {
      colorLog(data, appName, 'green')
    })

    process.stderr.on('data', function (data) {
      colorLog(data, appName, 'yellow')
    })

    process.on('exit', function (code) {
      colorLog(`child process exited with code ${code}`, appName, 'magenta')
    })
  }, timeout)
  timeout += 500
})

